
//
//  HTDataPickerView.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/1.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTDataPickerView.h"

@interface HTDataPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic,strong) NSArray *timeData;
@property (nonatomic,strong) NSArray *compareTime;

@property (nonatomic,assign) NSInteger currentSectionIndex;
@property (nonatomic,strong) NSMutableArray *section1Data;
@property (nonatomic,strong) NSMutableArray *section1ResultData;
@property (nonatomic,strong) NSMutableArray *section2Data;

@property (nonatomic,strong) NSMutableArray *todayData;
@property (nonatomic,strong) NSMutableArray *otherData;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@end


@implementation HTDataPickerView

- (void)awakeFromNib{
    [super awakeFromNib];
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    self.currentSectionIndex = 0;
    self.timeData = @[@"凌晨(0:00-6:00)",@"上午(6:00-12:00)",@"下午(12:00-18:00)",@"晚上(18:00-24:00)"];
    self.compareTime = @[@"6-00",@"12-00",@"18-00",@"24-00"];
    [self initData];
}
- (void)initData{
    [self.section1Data removeAllObjects];
    [self.section1Data addObject:@"随时装货"];
    [self.section1ResultData removeAllObjects];
    [self.section1ResultData addObject:@"随时装货"];
    for (int i = 0; i < 7; i ++) {
        NSArray *array = [self getAfterDate:i];
        [self.section1ResultData addObject:[NSString stringWithFormat:@"%@%@",array[1],array[2]]];
        if (i == 0) {
            [self.section1Data addObject:[NSString stringWithFormat:@"%@%@(今天)",array[1],array[2]]];
            continue;
        }
        if (i == 1) {
            [self.section1Data addObject:[NSString stringWithFormat:@"%@%@(明天)",array[1],array[2]]];
            continue;
        }
        [self.section1Data addObject:[NSString stringWithFormat:@"%@%@",array[1],array[2]]];
    }
    [self.todayData removeAllObjects];
    [self.todayData addObject:@"全天"];
    NSString *currentTime = [self getCurrentTime];
    for (int i = 0; i< self.compareTime.count; i++) {
        int result = [self compareDate:currentTime withDate:self.compareTime[i]];
        if (result >= 0) {
            [self.todayData addObject:self.timeData[i]];
        }
    }
    [self.otherData removeAllObjects];
    [self.otherData addObject:@"全天"];
    for (NSString *time in self.timeData) {
         [self.otherData addObject:time];
    }
}
- (NSMutableArray *)todayData{
    if (!_todayData) {
        _todayData = [NSMutableArray array];
    }
    return _todayData;
}
- (NSMutableArray *)otherData{
    if (!_otherData) {
        _otherData = [NSMutableArray array];
    }
    return _otherData;
}
- (NSMutableArray *)section1Data{
    if (!_section1Data) {
        _section1Data = [NSMutableArray array];
    }
    return _section1Data;
}
- (NSMutableArray *)section2Data{
    if (self.currentSectionIndex == 1) {
        _section2Data = self.todayData;
    }else{
        _section2Data = self.otherData;
    }
    return _section2Data;
}
- (NSMutableArray *)section1ResultData{
    if (!_section1ResultData) {
        _section1ResultData = [NSMutableArray array];
    }
    return _section1ResultData;
}
- (NSString *)resultData{
    NSInteger firstRow = [self.pickerView selectedRowInComponent:0];
    NSInteger sectondRow = [self.pickerView selectedRowInComponent:1];
    if (firstRow >= self.section1ResultData.count || firstRow < 0) {
        firstRow = 0;
    }
    if (sectondRow >= self.section1ResultData.count || sectondRow < 0) {
        sectondRow = 0;
    }
    NSString *time =self.section2Data[sectondRow] ;
    if (time != nil && time.length >= 2) {
        time = [time substringToIndex:2];
    }
   return [NSString stringWithFormat:@"%@,%@",self.section1ResultData[firstRow], time];
}
#pragma mark pickerviewDelegate
//返回列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//返回每列行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        
        return  self.section1Data.count;
        
    } else if(component==1){
        return self.section2Data.count;
    }
    return 0;
}
//每行高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component==0) {
        return  self.section1Data[row];
        
    } else if(component==1){
        return self.section2Data[row];
    }
    return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.currentSectionIndex = row;
        [pickerView reloadComponent:1];
    }
    if (self.currentSectionIndex == 0 && component == 1 && row != 0 ) {//随时装货
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
    
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    pickerLabel.textColor = [UIColor blackColor];
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}



#pragma mark --- 日期处理
//获取每个月有几天
- (NSString *)getDayNumber:(NSInteger)year month:(NSInteger)month{
    NSArray *days = @[@"31", @"28", @"31", @"30", @"31", @"30", @"31", @"31", @"30", @"31", @"30", @"31"];
    if (2 == month && 0 == (year % 4) && (0 != (year % 100) || 0 == (year % 400))) {
        return @"29";
    }
    return days[month - 1];
}

//获取7天的日期
- (NSArray *)getAfterDate:(NSInteger)n{
    
     NSDate*nowDate = [NSDate date];
     NSDate* theDate;
    if(n != 0){
        NSTimeInterval  oneDay = 24*60*60*1;
        theDate = [nowDate initWithTimeIntervalSinceNow: oneDay * n];
    }else{
         theDate = nowDate;
    }
    NSArray *array = [NSArray array];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年,MM月,dd日"];
    NSString *time = [formatter stringFromDate:theDate];
    array = [time componentsSeparatedByString:@","];
    return array;
}
// 获取当前的时分
- (NSString *)getCurrentTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH-mm"];
    NSDate *date = [NSDate date];
    NSString *time = [formatter stringFromDate:date];
    return time;
}
// 比较选择的时间
- (int)compareDate:(NSString *)date01 withDate:(NSString *)date02{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH-mm"];
    NSDate *dt1 = [df dateFromString:date01];
    NSDate *dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result) {
            //date02比date01大
        case NSOrderedAscending: ci=1;break;
            //date02比date01小
        case NSOrderedDescending: ci=-1;break;
            //date02=date01
        case NSOrderedSame: ci=0;break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1);break;
    }
    return ci;
}


       
@end
